package lpg;

class Stacks
{
    //
    // Given a rule of the form     A ::= x1 x2 ... xn     n > 0
    //
    // the function GETTOKEN(i) yields the symbol xi, if xi is a terminal
    // or ti, if xi is a nonterminal that produced a string of the form
    // xi => ti w.
    //
    protected int getToken(int i)
    {
        return locationStack[stateStackTop + (i - 1)];
    }

    //
    // Allocate or reallocate all the stacks. Their sizes should always be the same.
    //
    protected void reallocateStacks()
    {
        int oldStackLength = stateStack.length;
        int stackLength = oldStackLength + STACK_INCREMENT;
        System.arraycopy(stateStack, 0, stateStack = new int[stackLength], 0, oldStackLength);
        System.arraycopy(locationStack, 0, locationStack = new int[stackLength], 0, oldStackLength);
    }

    protected int stateStackTop     = -1;
    protected int[] stateStack      = new int[STACK_INCREMENT];
    protected int[] locationStack   = new int[STACK_INCREMENT];

    private static final int STACK_INCREMENT = 256;

}
